package com.antu.first.web.frontend;

import com.alibaba.fastjson.JSON;
import com.antu.first.bean.*;
import com.antu.first.dto.CommentOperationExecution;
import com.antu.first.dto.IndentOperationExecution;
import com.antu.first.enums.IndentStateEnum;
import com.antu.first.service.CommentService;
import com.antu.first.service.IndentService;
import com.antu.first.util.HttpServletRequestUtil;
import com.antu.first.util.IndentSequenceCreator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 对订单进行处理的Controller
 * Created by Administrator on 2018/3/5.
 */
@Controller
@RequestMapping("/frontend")
public class IndentController {

    @Autowired
    private IndentService indentService;
    @Autowired
    private CommentService commentService;

    /**
     * 小程序增加订单，需要传入的信息包括：
     * 入住时间、离开时间、入住人姓名、入住人联系电话、订单金额、实付金额、酒店Id、房间Id、商家Id、优惠券Id
     * 用户新增订单的时候，订单创建时间、订单状态以及订单编号由系统内部创建，用户信息由Session获取
     * @param indent
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addIndent", method = RequestMethod.GET)
    public String addIndent(Indent indent, HttpServletRequest request){
        Map<String,Object> modelMap =new HashMap<String,Object>();
        try{
            //模拟酒店信息
            Hotel hotel = new Hotel();
            hotel.sethId(3);
            //模拟房间信息
            Room room = new Room();
            room.setrId(1);
            //TODO 从Session中获取当前用户的信息
            //User user = (User) request.getSession().getAttribute("user");
            //模拟商家信息
            Merchant merchant = new Merchant();
            merchant.setmId(6);
            //模拟用户信息
            User user = new User();
            user.setuId(1);
            //模拟优惠券信息
            DiscountCoupon discountCoupon = new DiscountCoupon();
            discountCoupon.setdId(1);
            //优惠券
            indent.setDiscountCoupon(discountCoupon);
            //商家
            indent.setMerchant(merchant);
            //酒店
            indent.setHotel(hotel);
            //房间
            indent.setRoom(room);
            //用户
            indent.setUser(user);
            IndentOperationExecution execution = indentService.insertIndent(indent);
            if(execution.getState() != 1){
                modelMap.put("success", false);
                modelMap.put("errMsg", execution.getStateInfo());
                return JSON.toJSONString(modelMap);
            }
            modelMap.put("success", true);
            modelMap.put("indent", execution.getIndent());
        }catch(Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", "网络异常，下单失败，请稍后重试！");
        }
        return JSON.toJSONString(modelMap);
    }

    /**
     * 对订单进行评论，需要传入评论内容、订单Id、酒店Id、评论类型（好评？差评？中评？）、评论星级
     * @param coContent
     * @param iId
     * @param hId
     * @param coType
     * @param coStar
     * @return
     */
    @RequestMapping(value = "/addComment", method = RequestMethod.GET)
    @ResponseBody
    public String discussComment(String coContent, int iId, int hId, int coType, int coStar){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try{
            //TODO 从Session中取出用户的个人信息
            User user = new User();
            user.setuId(1);

            Hotel hotel = new Hotel();
            hotel.sethId(hId);
            Indent indent = new Indent();
            indent.setiId(iId);
            Comment comment = new Comment();
            comment.setUser(user);
            comment.setCoContent(coContent);
            comment.setCoType(coType);
            comment.setCoStar(coStar);
            comment.setHotel(hotel);
            comment.setIndent(indent);
            CommentOperationExecution execution = commentService.addComment(comment);
            if(execution.getState() != 6){
                modelMap.put("success", false);
                modelMap.put("errMsg", execution.getStateInfo());
                return JSON.toJSONString(modelMap);
            }
            modelMap.put("success", true);
            modelMap.put("comment", execution.getComment());
        }catch(Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", "程序出现异常，新增评论失败！");
        }
        return JSON.toJSONString(modelMap);
    }

    /**
     * 根据用户编号查询订单
     * @param user
     * @return
     */
    @RequestMapping(value = "/getIndentListByIUserd",method = RequestMethod.GET)
    @ResponseBody
    public String getIndentListByIUserd(User user){
        Map<String, Object> map = new HashMap<String, Object>();
        IndentOperationExecution indentOperationExecution=indentService.getIndentListByIUserd(user);
        if(indentOperationExecution!=null){
            map.put("message",true);
            map.put("indentOperationExecution",indentOperationExecution);
        }else{
            map.put("message",false);
            map.put("no","错误");
        }
        return JSON.toJSONString(map);


    }

    @RequestMapping(value = "/getIndentsByIdAndStatus")
    @ResponseBody
    public String queryIndent(int iStatus, int pageIndex, HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try{
            User user = (User) request.getSession().getAttribute("user");
            IndentOperationExecution execution = indentService.queryIndents(user.getuId(), iStatus, pageIndex);
            System.out.println(execution.getStateInfo());
            if(execution.getState() != 6){
                modelMap.put("success", false);
                modelMap.put("errMsg", execution.getStateInfo());
                System.out.println(modelMap);
                return JSON.toJSONString(modelMap);
            }
            modelMap.put("success", true);
            modelMap.put("indents", execution.getIndents());
        }catch(Exception e){
            modelMap.put("success", false);
            modelMap.put("errMsg", "获取订单失败，"+e.getMessage());
        }
        System.out.println(modelMap);
        return JSON.toJSONString(modelMap);
    }

}
